import 'entity_factory.dart';

///
class BaseListEntity<T> {
  int code;
  String msg;
  Data<T> data;

  ///
  BaseListEntity({this.code, this.msg, this.data});

  factory BaseListEntity.fromJson(json) {
    // List<T> mData = [];
    // if (json["data"] != null) {
    //   //遍历data并转换为我们传进来的类型
    //   (json["data"] as List).forEach((v) {
    //     mData.add(EntityFactory.generateOBJ<T>(v));
    //   });
    // }

    return BaseListEntity(
      code: json["code"] as int,
      msg: json["msg"] as String,
      data: Data.fromJson(json["data"] as Map<String, dynamic>),
    );
  }
}

class Data<T> {
  Data({
    this.list,
  });

  factory Data.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }
    final List<T> list = jsonRes['list'] is List ? <T>[] : null;
    if (list != null) {
      for (final dynamic item in jsonRes['list']) {
        if (item != null) {
          // LogPrint.getInstance().printInfo(item.toString());
          list.add(EntityFactory.generateOBJ<T>(item));
        }
      }
    }

    return Data(
      list: list,
    );
  }

  List<T> list;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'list': list,
      };
}
